import java.util.ArrayList;
import java.util.Scanner;

// 学生类
class Student {
    private int id;
    private String name;
    private int score;

    public Student(int id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return String.format("| %-4d | %-15s | %-6d |", id, name, score);
    }
}

// 学生成绩管理系统
public class StudentManagementSystem {
    private static ArrayList<Student> students = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        initializeStudents(); // 初始化学生数据
        boolean running = true;

        while (running) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 处理换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    viewStudents();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    running = false;
                    break;
                default:
                    System.out.println("⚠️ 无效的选择，请重新输入。");
            }
        }
    }

    private static void showMenu() {
        System.out.println("\n=========================");
        System.out.println("🎓 学生成绩管理系统 🎓");
        System.out.println("=========================");
        System.out.println("1. ➕ 添加学生");
        System.out.println("2. 📋 查看学生");
        System.out.println("3. ✏️ 更新学生");
        System.out.println("4. ❌ 删除学生");
        System.out.println("5. 🚪 退出");
        System.out.println("=========================");
        System.out.print("请输入您的选择：");
    }

    private static void addStudent() {
        System.out.print("请输入学生ID：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 处理换行符
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生成绩：");
        int score = scanner.nextInt();
        scanner.nextLine(); // 处理换行符

        students.add(new Student(id, name, score));
        System.out.println("✅ 学生添加成功！");
    }

    private static void viewStudents() {
        if (students.isEmpty()) {
            System.out.println("⚠️ 没有学生记录。");
        } else {
            System.out.println("\n=========================================");
            System.out.println("| ID   | 姓名             | 成绩   |");
            System.out.println("=========================================");
            for (Student student : students) {
                System.out.println(student);
            }
            System.out.println("=========================================");
        }
    }

    private static void updateStudent() {
        System.out.print("请输入要更新的学生ID：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 处理换行符

        Student student = findStudentById(id);
        if (student == null) {
            System.out.println("⚠️ 未找到学生ID。");
            return;
        }

        System.out.print("请输入新的学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入新的学生成绩：");
        int score = scanner.nextInt();
        scanner.nextLine(); // 处理换行符

        student.setName(name);
        student.setScore(score);
        System.out.println("✅ 学生更新成功！");
    }

    private static void deleteStudent() {
        System.out.print("请输入要删除的学生ID：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 处理换行符

        Student student = findStudentById(id);
        if (student == null) {
            System.out.println("⚠️ 未找到学生ID。");
            return;
        }

        students.remove(student);
        System.out.println("❌ 学生删除成功！");
    }

    private static Student findStudentById(int id) {
        for (Student student : students) {
            if (student.getId() == id) {
                return student;
            }
        }
        return null;
    }

    private static void initializeStudents() {
        students.add(new Student(1, "张三", 95));
        students.add(new Student(2, "李四", 88));
        students.add(new Student(3, "王五", 92));
        students.add(new Student(4, "赵六", 85));
        students.add(new Student(5, "钱七", 90));
        students.add(new Student(6, "孙八", 93));
        students.add(new Student(7, "周九", 89));
        students.add(new Student(8, "吴十", 87));
        students.add(new Student(9, "郑十一", 91));
        students.add(new Student(10, "王十二", 94));
    }
}
